package com.example.mannortycoon.controller;

import com.example.mannortycoon.entity.TradeEntity;
import com.example.mannortycoon.service.TradeService;
import com.example.mannortycoon.uitl.JWTUtils;
import com.example.mannortycoon.uitl.R;
import com.example.mannortycoon.vo.TradeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Path;
import java.util.List;

@RestController
@RequestMapping("/trade")
@Api(tags = "交易")
public class TradeController {

    private final TradeService tradeService;

    @Autowired
    public TradeController(TradeService tradeService){
        this.tradeService = tradeService;
    }

    /**
     * 获取交易列表
     *
     * @param type 列表类型 0：交易列表 1：用户的上架列表
     * @return 交易列表
     */
    @ApiOperation("获取交易列表")
    @GetMapping("/{type}")
    public R list(HttpServletRequest request,@PathVariable("type") Integer type){
        Long userId = JWTUtils.getId(request.getHeader("token"));
        List<TradeVO> list = tradeService.listTrade(userId,type);
        return R.ok().put("data",list);
    }

    @ApiOperation("用户上架")
    @PostMapping("/put")
    public R put(HttpServletRequest request, @RequestBody TradeEntity tradeEntity){
        Long userId = JWTUtils.getId(request.getHeader("token"));
        return tradeService.put(userId,tradeEntity);
    }

    /**
     * 下架交易
     *
     * @param id 交易ID
     */
    @ApiOperation("下架")
    @DeleteMapping("/takeDown/{id}")
    public R takeDown(@PathVariable("id") Long id){
        tradeService.removeById(id);
        return R.ok();
    }

    @ApiOperation("交易")
    @PostMapping("/{id}/{number}")
    public R trade(HttpServletRequest request,@PathVariable("id") Long id,@PathVariable("number") Integer number){
        Long userId = JWTUtils.getId(request.getHeader("token"));
        return tradeService.trade(userId,id,number);
    }
}
